import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);
const originalPush = Router.prototype.push
Router.prototype.push = function push (location) {
    // return (() => {
    //   const push = originalPush.bind(this)
    //   try {
    //     // console.log(location)
    //     push(location)
    //   } catch (err) {
    //     console.log(err)
    //   }
    // })()
    return originalPush.call(this, location).catch(err => err);
};


/* 路由模块 */
import Layout from "@/layout";

const main = () => import("@/views/redirect/index.vue");
const login = () => import("@/views/login/index.vue");


export const constantRouterMap = [
  {
    path: "/login",
    component: login,
    hidden: true,
    props: (route) => ({ query: route.query.mode }),
  },
  {
    path: "/iframe",
    component: () => import("@/views/iframe/index.vue"),
    props: (route) => ({ query: route.query.code })
  },
  {
    path: '/',
    component: login
  },
  {
    path: '/page403',
    component: () => import("@/views/page403.vue"),
  },
  {
    path: '',
    component: login
  },{
    path: "/bigScreen",
    component: () => import("@/views/bigScreen/indexNew.vue"),
    name: "bigScreen",
  },
  {
    path: "/workPlace",
    component: () => import("@/views/workPlace/index.vue"),
    name: "workPlace",
  },{
    path: "/recordCard",
    component: () => import("@/views/recordCard/index.vue"),
    name: "recordCard",
  },
  {
    path: "/FirstPage",
    component: Layout,
    redirect: "/FirstPage",
    meta:{permission:"home:index"},
    children: [
      {
        path: "index",
        component: () => import("@/views/FirstPage/index.vue"),
        name: "index",
        meta: { title: "主页",iconNew: "icon-shouye", affix: true ,permission:"see:home"}
      }
    ]
  },
  {
    path: "/sending-station",
    component: Layout,
    name: "sending-station",
    meta: {
      title: "送检",
      icon: "sending_station",
      iconNew: "icon-kaoqin",
      permission:"send:index",
      image:require('@/assets/img/send.png')
    },
    children: [
      {
        path: "sending",
        component: () => import("@/views/sending/sending.vue"),
        name: "sending",
        choose: true,
        meta: { title: "送检" ,permission:"send:index",image:require('@/assets/img/send-01.png'),iconNew: "icon-bottle_1px"}
      },
    ]
  },
  {
    path: "/sample-detection",
    component: Layout,
    name: "sample-detection",
    meta: {
      title: "检验",
      icon: "inspection_area",
      iconNew: "icon-jianyan_line",
      permission:"check:index",
      image:require('@/assets/img/sample.png')
    },
    children: [
      {
        path: "detection",
        component: () => import("@/views/inspection/sample-detection.vue"),
        name: "detection",
        choose: true,
        meta: { title: "检验" , permission:"check:index",image:require('@/assets/img/sample-check.png'),iconNew: "icon-shijiyanshou"}
      }
    ]
  },
  {
    path: "/approval",
    component: Layout,
    meta:{permission:"approve:index",image:require('@/assets/img/approve.png') },
    children: [
      {
        path: "approval",
        component: () => import("@/views/approval/index.vue",),
        name: "approval",
        choose: true,
        meta: { title: "审批", icon: "approval",permission:"approve:index" ,image:require('@/assets/img/approve.png'),iconNew: "icon-shenpi"}
      }
    ]
  },
  {
    path: "/sample-management",
    component: Layout,
    meta: {permission:"saveSample:index",image:require('@/assets/img/sample-save.png') },
    children: [
        {
          path: "save",
          component: () => import("@/views/sample-management/sample-management.vue",),
          name: "save",
          choose: true,
          meta: { title: "存样",permission:"saveSample:index",image:require('@/assets/img/sample-save.png'),iconNew: "icon-yangpinliuyang" },
        }
      ]
  },
  {
    path: "/check",
    component: Layout,
    name: "check",
    meta: {
      title: "检效",
      icon: "sending_station",
      iconNew: "icon-kaoqin",
      permission:"effiect:index",
      image:require('@/assets/img/send.png')
    },
    children: [
      {
        path: "check",
        component: () => import("@/views/check/index.vue"),
        name: "check",
        choose: true,
        meta: { title: "绩效分析", icon: "check" ,permission:"anay:effiect",image:require('@/assets/img/check.png'),iconNew: "icon-xiaoshuaiguanli" }
      },
      {
        path: "recordPane",
        component: () => import("@/views/check/components/RecordPane.vue"),
        name: "recordPane",
        choose: true,
        meta: { title: "检单复打", icon: "check" ,permission:"print:effiect",image:require('@/assets/img/check.png'),iconNew: "icon-xiaoshuaiguanli" }
      },
    ]
  },
  {
    path: "/param",
    component: Layout,
    name: "param",
    meta: {
      title: "基础数据",
      icon: "param",
      iconNew: "icon-canshushezhi",
      permission:"basedata:index",
      image:require('@/assets/img/param.png')
    },
    children: [
      {
        path: "notice",
        component: () => import("@/views/system/notice.vue"),
        name: "notice",
        choose: true,
        meta: { title: "信息发布",permission:"notice:basedata",image:require('@/assets/img/info-send.png'),iconNew: "icon-fabufuwu" }
      },
      {
        path: "material-coding",
        component: () => import("@/views/sending/material-coding.vue"),
        name: "material-coding",
        choose: true,
        meta: { title: "物料编码",permission:"material:basedata",image:require('@/assets/img/send-02.png'),iconNew: "icon-gongzuotaiwuliaomingcheng" }
      },
      {
        path: "quality-standard",
        component: () => import("@/views/inspection/quality-standard.vue"),
        name: "quality-standard",
        choose: true,
        meta: { title: "质量标准",permission:"standard:basedata",image:require('@/assets/img/material.png'),iconNew: "icon-zhiliang" }
      },
      {
        path: "test-method",
        component: () => import("@/views/inspection/test-method.vue"),
        name: "test-method",
        choose: true,
        meta: { title: "检验方法",permission:"method:basedata",image:require('@/assets/img/method.png'),iconNew: "icon-jianyanfangfa" }
      },

      {
        path: "inspection-specification",
        component: () => import("@/views/param/inspection-specification.vue"),
        name: "inspection-specification",
        choose: true,
        meta: { title: "检验规则" , permission:"rule:basedata",image:require('@/assets/img/param-rule.png'),iconNew: "icon-miaopubianmashengcheng"}
      },
      {
        path: "param-manage",
        component: () => import("@/views/param/index.vue"),
        name: "param-manage",
        choose: true,
        meta: { title: "参数管理" ,permission:"param:basedata",image:require('@/assets/img/param-manage.png'),iconNew: "icon-canshu"}
      }
    ]
  },
  {
    path: "/system",
    component: Layout,
    name: "system",
    meta: {
      title: "系统",
      icon: "system",
      iconNew: "icon-shezhi",
      permission:"system:index",
      image:require('@/assets/img/system-main.png')
    },
    children: [
      {
        path: "user",
        component: () => import("@/views/system/user.vue"),
        name: "user",
        choose: true,
        meta: { title: "用户管理",permission:"user:system" ,image:require('@/assets/img/usermanage.png'),iconNew: "icon-yonghu" }
      },
      {
        path: "organization",
        component: () => import("@/views/system/organization.vue"),
        name: "organization",
        choose: true,
        meta: { title: "组织管理",permission:"org:system",image:require('@/assets/img/orgmanage.png'),iconNew: "icon-zuzhijiegou"  }
      },
      {
        path: "role",
        component: () => import("@/views/system/role.vue"),
        name: "role",
        choose: true,
        meta: { title: "角色权限管理" ,permission:"role:system",image:require('@/assets/img/rolemanage.png'),iconNew: "icon-quanxianshenyue"}
      },
      {
        path: "logs",
        component: () => import("@/views/system/logs.vue"),
        name: "logs",
        choose: true,
        meta: { title: "系统日志" ,permission:"log:system",image:require('@/assets/img/logmanage.png'),iconNew: "icon-wj-rz"}
      },
      {
        path: "backup",
        component: () => import("@/views/system/backup.vue"),
        name: "backup",
        choose: true,
        meta: { title: "数据备份",permission:"copy:system" ,image:require('@/assets/img/sysback.png'),iconNew: "icon-bianzubeifen"}
      },
      {
        path: "picture",
        component: () => import("@/views/system/picture.vue"),
        name: "picture",
        choose: true,
        meta: { title: "视觉识别",permission:"images:system" ,image:require('@/assets/img/pictureIcon.png'),iconNew: "icon-tupian"}
      }
    ]
  }
];

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: constantRouterMap
  });

const router = createRouter();

//*************调用的addRoutes()方法是router自带的原生方法，是动态添加路由的，它并没有删除之前路由中原有的路由！！************//
//*************将permission.js中的router.addRoutes替换成router.selfaddRoutes即可************//
router.selfaddRoutes = function (params){
  router.matcher = new Router().matcher;
  router.addRoutes(params)
}

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}
export const asyncRouterMap = [
  { path: '*', redirect: '/404', hidden: true }
]

export default router;
